package com.mashibing.juc.c_007;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName T04_LockSupport
 * @Description LockSupport
 * @Author xiongwei.wu
 * @Date 2021/7/12 17:21
 **/
public class T04_LockSupport {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                System.out.println(i);
                if (i == 5) {
                    //当前线程停止运行
                    LockSupport.park();
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });


        thread.start();

        //unpack可以先于pack使用，用法比wait,unWait更灵活
        LockSupport.unpark(thread);

        try {
            //阻塞5秒
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("after 8 seconds...");

        //解封
//        LockSupport.unpark(thread);
    }
}
